{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Qubit Mapping\n",
    "\n",
    "在 MindQuantum 0.9.0 中，我们正式加入对不同芯片架构的支持。我们引入 `QubitsTopology` 来描述芯片中比特之间的拓扑关系， 引入 `QubitNode` 来描述单个的比特。\n",
    "\n",
    "## Qubit Node\n",
    "\n",
    "当前，QubitNode 主要包含四个属性：\n",
    "\n",
    "- id：量子比特的唯一识别码\n",
    "- color：在绘制拓扑结构图时，量子比特的颜色， 默认为 #000000\n",
    "- poi_x：在绘制拓扑结构图时，量子比特的 x 位置，默认为 0.0\n",
    "- poi_y：在绘制拓扑结构图时，量子比特的 y 位置，默认为 0.0\n",
    "\n",
    "下面，我们申明一个量子比特："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[WARNING] ME(35581:139977989084480,MainProcess):2023-07-14-14:10:39.573.101 [mindspore/run_check/_check_version.py:102] MindSpore version 2.0.0rc1 and cuda version 11.8.89 does not match, CUDA version [['10.1', '11.1', '11.6']] are supported by MindSpore officially. Please refer to the installation guide for version matching information: https://www.mindspore.cn/install.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "qubit id: 0, with color: #121212, and position (0, 0)\n"
     ]
    }
   ],
   "source": [
    "from mindquantum.device import QubitNode\n",
    "\n",
    "q0 = QubitNode(0, '#121212', 0, 0)\n",
    "\n",
    "print(f\"qubit id: {q0.qubit_id}, with color: {q0.color}, and position ({q0.poi_x}, {q0.poi_y})\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "量子比特之间可以通过运算符 `>>` 和 `<<` 来将两个比特连接起来，也可以同过 `>` 和 `<` 取消连接。不同方向的连接或者取消连接算符会有不同的返回值。\n",
    "\n",
    "|lhs|op|rhs|效果|返回值|\n",
    "| --|--|-- |-- |--|\n",
    "|q0 |`>>`|q1 |连接两个比特|q1|\n",
    "|q0 |`<<`|q1 |连接两个比特|q0|\n",
    "|q0 |`>`|q1 |取消连接两个比特|q1|\n",
    "|q0 |`<`|q1 |取消连接两个比特|q0|\n",
    "\n",
    "同过如上定义的操作，我们可以快速连接不同的比特，如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mindquantum.device.topology.QubitNode at 0x7f4f2459bc10>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q0, q1, q2 = QubitNode(0), QubitNode(1), QubitNode(2)\n",
    "\n",
    "q0 >> q1 >> q2"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在上述代码中 `q0 >> q1` 会将 `q0` 和 `q1` 比特连接起来，并返回 `q1` 比特，而后再同过 `>> q2` 就可以将 `q1` 和 `q2` 连接起来。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### QubitsTopology\n",
    "\n",
    "`QubitsTopology` 类是量子比特的容器，可以有效的组织和操控量子比特，例如当我们想要构建一个一维链的量子比特时，我们可以如下操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<mindquantum.device.topology.QubitsTopology object at 0x7f4f26b43b80>\n"
     ]
    }
   ],
   "source": [
    "from mindquantum.device import QubitsTopology, QubitNode\n",
    "\n",
    "n = 5\n",
    "\n",
    "topology = QubitsTopology([QubitNode(i, poi_x=i) for i in range(n)])\n",
    "print(topology)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "MindQuantum 中自带类绘制量子比特拓扑结构图的接口，接口如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"264\" height=\"24.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"264\" height=\"24.0\" fill=\"#ffffff\" />\n",
       "\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "3\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "4\n",
       " </text>\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGContainer at 0x7f4f2459bac0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.io.display import draw_topology\n",
    "\n",
    "draw_topology(topology)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由上我们可以发现，我们成功产生类 5 个量子比特，但是还没有将其连接起来，下面我们就来将其进行耦合。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"264\" height=\"24.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"264\" height=\"24.0\" fill=\"#ffffff\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"132.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"192.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"252.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "3\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "4\n",
       " </text>\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGContainer at 0x7f4eb1299130>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left_node = topology[0]\n",
    "for i in range(1, n):\n",
    "    left_node = left_node << topology[i]\n",
    "\n",
    "draw_topology(topology)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在上述代码中，我们通过取值运算符 `[]`，获取对应 id 的量子比特，然后我们利用 `<<` 运算符进行比特之间的连接。\n",
    "\n",
    "如上，我们便生成了一维链的量子比特。在 MindQuantum 中，我们已经集成类多种结构，如这里提到的一位链结构，和方格子结构："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"144\" height=\"24.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"144\" height=\"24.0\" fill=\"#ffffff\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "</svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"264\" height=\"204\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"264\" height=\"204\" fill=\"#ffffff\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"72.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"72.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"72.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"192.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"192.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"192.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "3\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "4\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "5\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "6\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "7\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "8\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "9\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "10\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "11\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "12\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "13\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "14\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "15\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "16\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "17\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "18\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "19\n",
       " </text>\n",
       "</svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from mindquantum.device import LinearQubits, GridQubits\n",
    "from IPython.display import display_svg\n",
    "\n",
    "t1 = LinearQubits(3)\n",
    "t2 = GridQubits(4, 5)\n",
    "\n",
    "display_svg(draw_topology(t1))\n",
    "display_svg(draw_topology(t2))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们还可以对拓扑结构图进行更多的操作，如删除某个节点："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"264\" height=\"204\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"264\" height=\"204\" fill=\"#ffffff\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"72.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"192.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"192.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"192.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "3\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "4\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "5\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "7\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "8\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "9\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "10\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "11\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "12\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "13\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "14\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "15\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "16\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "17\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "18\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "19\n",
       " </text>\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGContainer at 0x7f4eb12a8c40>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2.remove_qubit_node(6)\n",
    "\n",
    "draw_topology(t2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "孤立某个节点："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"264\" height=\"204\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"264\" height=\"204\" fill=\"#ffffff\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"72.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"192.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "3\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "4\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "5\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "7\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "8\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "9\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "10\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "11\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "12\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "13\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "14\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "15\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "16\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "17\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "18\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "19\n",
       " </text>\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGContainer at 0x7f4eb1231af0>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2.isolate_with_near(13)\n",
    "\n",
    "draw_topology(t2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "修改节点颜色："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"264\" height=\"204\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"264\" height=\"204\" fill=\"#ffffff\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"72.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"192.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "3\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "4\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "5\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"72.0\" r=\"12\" fill=\"#ff0000\" />\n",
       "<text x=\"132.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "7\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "8\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "9\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "10\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "11\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "12\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "13\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "14\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "15\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "16\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "17\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "18\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "19\n",
       " </text>\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGContainer at 0x7f4eb12417c0>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2.set_color(7, '#ff0000')\n",
    "\n",
    "draw_topology(t2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "修改节点位置："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"324.0\" height=\"204\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"324.0\" height=\"204\" fill=\"#ffffff\" />\n",
       "<line x1=\"192.0\" x2=\"312.0\" y1=\"12.0\" y2=\"42.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"312.0\" x2=\"252.0\" y1=\"42.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"72.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"192.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "3\n",
       " </text>\n",
       "<circle cx=\"312.0\" cy=\"42.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"312.0\" y=\"42.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "4\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "5\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"72.0\" r=\"12\" fill=\"#ff0000\" />\n",
       "<text x=\"132.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "7\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "8\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "9\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "10\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "11\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "12\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "13\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "14\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "15\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "16\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "17\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "18\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "19\n",
       " </text>\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGContainer at 0x7f4eb12503a0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2.set_position(4, 5.0, 0.5)\n",
    "\n",
    "draw_topology(t2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "重新耦合比特："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"324.0\" height=\"204\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"324.0\" height=\"204\" fill=\"#ffffff\" />\n",
       "<line x1=\"192.0\" x2=\"312.0\" y1=\"12.0\" y2=\"42.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"312.0\" x2=\"252.0\" y1=\"42.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"252.0\" x2=\"252.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"252.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"312.0\" x2=\"252.0\" y1=\"42.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"72.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"192.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"72.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"192.0\" x2=\"192.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"132.0\" y1=\"132.0\" y2=\"192.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"132.0\" y1=\"132.0\" y2=\"132.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"132.0\" x2=\"192.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "3\n",
       " </text>\n",
       "<circle cx=\"312.0\" cy=\"42.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"312.0\" y=\"42.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "4\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "5\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"72.0\" r=\"12\" fill=\"#ff0000\" />\n",
       "<text x=\"132.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "7\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "8\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "9\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "10\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "11\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "12\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "13\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"132.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"132.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "14\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "15\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "16\n",
       " </text>\n",
       "<circle cx=\"132.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"132.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "17\n",
       " </text>\n",
       "<circle cx=\"192.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"192.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "18\n",
       " </text>\n",
       "<circle cx=\"252.0\" cy=\"192.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"252.0\" y=\"192.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "19\n",
       " </text>\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGContainer at 0x7f4eb125b220>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2[4] << t2[14]\n",
    "\n",
    "draw_topology(t2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们还可以获取拓扑结构中所有的耦合边："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(0, 1),\n",
       " (0, 5),\n",
       " (1, 2),\n",
       " (2, 3),\n",
       " (2, 7),\n",
       " (3, 4),\n",
       " (3, 8),\n",
       " (4, 9),\n",
       " (4, 14),\n",
       " (5, 10),\n",
       " (7, 8),\n",
       " (7, 12),\n",
       " (8, 9),\n",
       " (9, 14),\n",
       " (10, 11),\n",
       " (10, 15),\n",
       " (11, 12),\n",
       " (11, 16),\n",
       " (12, 17),\n",
       " (14, 19),\n",
       " (15, 16),\n",
       " (16, 17),\n",
       " (17, 18),\n",
       " (18, 19)}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2.edges_with_id()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Qubit Mapping\n",
    "\n",
    "当我们在真实量子硬件上运行量子线路时，我们往往不能直接运行，因为真实硬件中的比特拓扑关系和用户给定的量子线路结构并不一定匹配，这是比特映射技术就应运而生。我们可以通过对量子比特进行重新映射，或插入一下 `SWAP` 门，来使得线路能够成功运行。\n",
    "\n",
    "我们给定一个量子线路："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"200.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<circle cx=\"212.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"192.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"198.8\" x2=\"226.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"258.8\" x2=\"286.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"26.0\" y2=\"54.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f4eb12a8e50>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.circuit import Circuit\n",
    "\n",
    "circ = Circuit().h(0).h(1).h(2).x(1, 0).x(2, 1).x(0, 2)\n",
    "circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "再给定一个 2x2 的方格子量子拓扑结构："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"84\" height=\"84\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"84\" height=\"84\" fill=\"#ffffff\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"72.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ababab\" stroke-width=\"8\" />\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "3\n",
       " </text>\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGContainer at 0x7f4eb11f4ee0>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = GridQubits(2, 2)\n",
    "\n",
    "draw_topology(t)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由于上述量子线路中的量子比特两两都有相互作用，因此无法直接在上述的量子硬件中运行。在 MindQuantum 0.9.0 中，我们引入 SABER 算法解决该问题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mindquantum.algorithm.mapping import SABRE\n",
    "\n",
    "solver = SABRE(circ, t)\n",
    "new_circ, init_mapping, final_mapping = solver.solve(5, 0.5, 0.3, 0.2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以看到新给出的量子线路为："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"260.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"260.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q3:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"138.8\" x2=\"166.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<circle cx=\"212.8\" cy=\"220.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"192.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"198.8\" x2=\"226.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n",
       "\n",
       "<rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n",
       "<path d=\"M 203.2 36.31384387633061 L 208.0 28.0 L 212.8 36.31384387633061 L 209.44 36.31384387633061 L 209.44 52.0 L 206.56 52.0 L 206.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n",
       "<path d=\"M 222.39999999999998 43.68615612366939 L 217.60000000000002 52.0 L 212.8 43.68615612366939 L 216.16000000000003 43.68615612366939 L 216.16000000000003 28.0 L 219.04000000000002 28.0 L 219.04000000000002 43.68615612366939 Z\" fill=\"#ffffff\" />\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n",
       "<path d=\"M 203.2 96.31384387633061 L 208.0 88.0 L 212.8 96.31384387633061 L 209.44 96.31384387633061 L 209.44 112.0 L 206.56 112.0 L 206.56 96.31384387633061 Z\" fill=\"#ffffff\" />\n",
       "<path d=\"M 222.39999999999998 103.68615612366939 L 217.60000000000002 112.0 L 212.8 103.68615612366939 L 216.16000000000003 103.68615612366939 L 216.16000000000003 88.0 L 219.04000000000002 88.0 L 219.04000000000002 103.68615612366939 Z\" fill=\"#ffffff\" />\n",
       "<circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"258.8\" x2=\"286.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"26.0\" y2=\"54.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f4eb126fcd0>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过对比旧的量子线路，我们得知，算法在运行完第二个CNOT门后发现，`q0` 和 `q3`没有直接耦合，因此把 `q0` 和 `q1` 上的量子态通过 `SWAP` 门互换，然后再作用第三个CNOT门。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"200.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"316.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<circle cx=\"212.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"192.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"198.8\" x2=\"226.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"258.8\" x2=\"286.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"26.0\" y2=\"54.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f4eb11fa610>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里 `init_mapping` 和 `final_mapping` 分别告诉我们，最开始量子线路应该如何作用在量子硬件上，以及运行完线路后，各量子比特与原始线路中量子比特的对应关系。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "initial mapping: [1, 3, 2, 0]\n",
      "  final mapping: [0, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "print(f\"initial mapping: {init_mapping}\")\n",
    "print(f\"  final mapping: {final_mapping}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们还可以通过 `draw_topology`，在量子拓扑图中绘制出新的量子线路用到了哪些量子比特以及其耦合的边。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"84\" height=\"84\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0\" width=\"84\" height=\"84\" fill=\"#ffffff\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"12.0\" y2=\"12.0\" stroke=\"#ff0000\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"12.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ff0000\" stroke-width=\"8\" />\n",
       "<line x1=\"12.0\" x2=\"72.0\" y1=\"72.0\" y2=\"72.0\" stroke=\"#ff0000\" stroke-width=\"8\" />\n",
       "<line x1=\"72.0\" x2=\"72.0\" y1=\"12.0\" y2=\"72.0\" stroke=\"#ff0000\" stroke-width=\"8\" />\n",
       "<circle cx=\"12.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "0\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"12.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"12.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "1\n",
       " </text>\n",
       "<circle cx=\"12.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"12.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "2\n",
       " </text>\n",
       "<circle cx=\"72.0\" cy=\"72.0\" r=\"12\" fill=\"#000000\" />\n",
       "<text x=\"72.0\" y=\"72.0\" font-size=\"18px\" dominant-baseline=\"middle\" text-anchor=\"middle\" fill=\"#ffffff\" >\n",
       "3\n",
       " </text>\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGContainer at 0x7f4eb12126a0>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "draw_topology(t, new_circ)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们发现，其中的4个量子比特和4条边全部都用上了。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
